package com.thtf.openguess.service.impl;

import com.google.common.base.Stopwatch;
import com.thtf.openguess.openguess.*;
import com.thtf.openguess.service.*;
import com.thtf.openguess.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import uk.co.jemos.podam.api.PodamFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {

    @Value("${const.virtual}")
    private String virtual;


        public String[] year = {"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010",
            "2011","2012","2013","2014","2015","2016","2017","2018","2019","2020","2021","2022"};
//    public String[] year = {"2022"};
    public String[] month = {"01","02","03","04","05","06","07","08","09","10","11","12"};



    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(List<C2016000000B20411> list, CountDownLatch countDownLatch, int size, C2016000000B20411Service service) {
        final Stopwatch started = Stopwatch.createStarted();
        long start = System.currentTimeMillis();
        service.saveBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("操作成功  耗费时间:"+ (end - start) / 1000 + " s");
        countDownLatch.countDown();
        log.info("插入"+size+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());
    }

    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(List<C2016000000B204121b> list, CountDownLatch countDownLatch, int size, C2016000000B204121bService service) {
        final Stopwatch started = Stopwatch.createStarted();
        service.saveBatch(list);
        countDownLatch.countDown();
        log.info("插入"+size+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());

    }

    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(List<C201600000020111> list, CountDownLatch countDownLatch, int size, C201600000020111Service service) {
        final Stopwatch started = Stopwatch.createStarted();
        long start = System.currentTimeMillis();
        service.saveBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("操作成功  耗费时间:"+ (end - start) / 1000 + " s");
        countDownLatch.countDown();
        log.info("插入"+size+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());
    }

    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(Integer boundNum, CountDownLatch countDownLatch, Integer sqlNum, PodamFactory podamFactory, C2016000000B20411Service c2016000000B20411Service, C2016000000B204121bService c2016000000B204121bService, C2016000000B204122bService c2016000000B204122bService, C2016000000B204123bService c2016000000B204123bService, C201600000020111Service c201600000020111Service) {
        final Stopwatch started = Stopwatch.createStarted();
        List<C2016000000B20411> c2016000000B20411s= new ArrayList<>();
        List<C201600000020111> c201600000020111s= new ArrayList<>();
        List<C2016000000B204121b> c2016000000B204121bs= new ArrayList<>();
        List<C2016000000B204122b> c2016000000B204122bs= new ArrayList<>();
        List<C2016000000B204123b> c2016000000B204123bs= new ArrayList<>();
        long start1 = System.currentTimeMillis();

        for (int i =0 ; i< sqlNum ;i++){
            //构建报告期字段，由
            String bgq = year[new Random().nextInt(year.length)]+month[new Random().nextInt(month.length-1)]+"MM";
            String zzjgdm = "zzjgdm_"+virtual+new Random().nextInt(300000000);
            String dcdxxtm =zzjgdm;

            c2016000000B20411s.add(Util.forEachSetValue(new C2016000000B20411()).setBgq(bgq).setZzjgdm(zzjgdm).setDcdxxtm(dcdxxtm));
            c201600000020111s.add(Util.forEachSetValue(new C201600000020111()).setBgq(bgq).setZzjgdm(zzjgdm).setDcdxxtm(dcdxxtm)
                    .setB205(String.valueOf(new Random().nextInt(10))).setB103b(String.valueOf(new Random().nextInt(10))).setB191(String.valueOf(new Random().nextInt(10))).setB206(String.valueOf(new Random().nextInt(10))));

            for (int j = 0 ; j<boundNum ; j ++){
                c2016000000B204121bs.add(Util.forEachSetValue(new C2016000000B204121b()).setBgq(bgq).setZzjgdm(zzjgdm).setDcdxxtm(dcdxxtm).setCode(String.valueOf(j)));
                c2016000000B204122bs.add(Util.forEachSetValue(new C2016000000B204122b()).setBgq(bgq).setZzjgdm(zzjgdm).setDcdxxtm(dcdxxtm).setCode(String.valueOf(j)));
                c2016000000B204123bs.add(Util.forEachSetValue(new C2016000000B204123b()).setBgq(bgq).setZzjgdm(zzjgdm).setDcdxxtm(dcdxxtm).setCode(String.valueOf(j)));
            }

        }
        long end1 = System.currentTimeMillis();
        System.out.println("装载sql  耗费时间:"+ (end1 - start1) / 1000 + " s");
        long start = System.currentTimeMillis();
        c2016000000B204121bService.saveBatch(c2016000000B204121bs,1000);
        c2016000000B204122bService.saveBatch(c2016000000B204122bs,1000);
        c2016000000B204123bService.saveBatch(c2016000000B204123bs,1000);
        c2016000000B20411Service.saveBatch(c2016000000B20411s,1000);
        c201600000020111Service.saveBatch(c201600000020111s,1000);
        long end = System.currentTimeMillis();
        System.out.println("执行sql  耗费时间:"+ (end - start) / 1000 + " s");
        countDownLatch.countDown();
        log.info("插入"+sqlNum+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());
    }

    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(List<C2016000000B204122b> list, CountDownLatch countDownLatch, int size, C2016000000B204122bService service) {
        final Stopwatch started = Stopwatch.createStarted();
        service.saveBatch(list);
        countDownLatch.countDown();
        log.info("插入"+size+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());
    }

    @Override
    @Async("asyncServiceExecutor")
    public void dealAdd(List<C2016000000B204123b> list, CountDownLatch countDownLatch, int size, C2016000000B204123bService service) {

        final Stopwatch started = Stopwatch.createStarted();
        service.saveBatch(list);
        countDownLatch.countDown();
        log.info("插入"+size+"数据耗时(ms) ============ " + started.elapsed(TimeUnit.MILLISECONDS) + "   计数减1 =======" + countDownLatch.getCount()+"当前线程"+Thread.currentThread().getId());
    }
}